﻿using System.Text;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    public class ConnectionInfo
    {
        private string server;

        private string userID;

        private string password;

        public string Server
        {
            get
            {
                return server;
            }
            set
            {
                server = value;
            }
        }

        public string UserId
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }

        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        public string ConnectionString
        {
            get
            {
                return string.Format("Data Source={0};User ID={1};Password={2}", server, userID, password);
            }
        }

        public ConnectionInfo()
        {
        }

        public ConnectionInfo(string connectionString)
        {
            server = GetSubItemValue(connectionString, "Data Source");
            if (server == null)
            {
                server = GetSubItemValue(connectionString, "server");
            }
            userID = GetSubItemValue(connectionString, "User ID");
            if (userID == null)
            {
                userID = GetSubItemValue(connectionString, "uid");
            }
            password = GetSubItemValue(connectionString, "Password");
            if (password == null)
            {
                password = GetSubItemValue(connectionString, "pwd");
            }
            if (password == null)
            {
                password = GetSubItemValue(connectionString, "Jet OLEDB:Database Password");
            }
        }

        private string GetSubItemValue(string itemValueString, string subKeyName)
        {
            string[] array = itemValueString.Split(';');
            for (int i = 0; i < array.Length; i++)
            {
                string text = array[i].ToLower();
                if (text.IndexOf(subKeyName.ToLower()) >= 0)
                {
                    int num = array[i].IndexOf("=");
                    return array[i].Substring(num + 1);
                }
            }
            return null;
        }

        private string EncodeBase64(string source)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(source);
            return Convert.ToBase64String(bytes, 0, bytes.Length);
        }
    }

}
